/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.im.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.im.bean.ImMsgBizEmployeeRecent;
import com.yami.shop.im.bean.ImMsgBizUser;
import com.yami.shop.im.constants.SendType;
import com.yami.shop.im.service.ImMsgBizEmployeeRecentService;
import com.yami.shop.im.service.ImMsgBizUserService;
import com.yami.shop.im.util.SecurityUtils;
import com.yami.shop.im.vo.Conversation;
import com.yami.shop.security.multishop.model.YamiShopUser;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Lzk
 */
@RestController
@RequestMapping("/p/shop")
@Api(tags = "店铺聊天接口")
@AllArgsConstructor
public class ShopController {

    @Autowired
    private UserService userService;
    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private ImMsgBizEmployeeRecentService imMsgBizEmployeeRecentService;
    @Autowired
    private ImMsgBizUserService imMsgBizUserService;

    @PostMapping("/conversations")
    @ApiOperation(value = "最后的对话信息列表项", notes = "获取最后的对话信息列表项")
    public ResponseEntity<Page<Conversation>> conversations(PageParam<ImMsgBizEmployeeRecent> pageParam) {
        YamiShopUser yamiShopUser = SecurityUtils.getShopUser();
        Page<ImMsgBizEmployeeRecent> imMsgBizEmployeeRecentPage = imMsgBizEmployeeRecentService.page(pageParam, new LambdaQueryWrapper<ImMsgBizEmployeeRecent>()
                .eq(ImMsgBizEmployeeRecent::getSendType, SendType.SHOP).eq(ImMsgBizEmployeeRecent::getBizId, yamiShopUser.getShopId()).eq(ImMsgBizEmployeeRecent::getEmployeeId, yamiShopUser.getEmployeeId()).orderByDesc(ImMsgBizEmployeeRecent::getUpdateTime));
        List<ImMsgBizEmployeeRecent> imMsgBizEmployeeRecentList = imMsgBizEmployeeRecentPage.getRecords();
        List<Conversation> conversations = new ArrayList<>();
        for (ImMsgBizEmployeeRecent imMsgBizEmployeeRecent : imMsgBizEmployeeRecentList) {
            ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(imMsgBizEmployeeRecent.getBizId());
            if (shopDetail != null && shopDetail.getShopStatus() != null) {
                ImMsgBizUser imMsgBizUser = imMsgBizUserService.getOne(new LambdaQueryWrapper<ImMsgBizUser>()
                        .eq(ImMsgBizUser::getSendType, SendType.SHOP).eq(ImMsgBizUser::getBizId, imMsgBizEmployeeRecent.getBizId()).eq(ImMsgBizUser::getUserId, imMsgBizEmployeeRecent.getUserId()));
                Conversation conversation = new Conversation();
                conversation.setChatType(imMsgBizUser.getSendType());
                conversation.setShopId(imMsgBizEmployeeRecent.getBizId());
                conversation.setUserId(imMsgBizEmployeeRecent.getUserId());
                conversation.setLatestMsg(imMsgBizUser.getLastMsgContent());
                conversation.setUnread(imMsgBizEmployeeRecent.getEmployeeUnreadNum());
                // todo 东8区，不应该写死"+8"后续优化
                conversation.setTimestamp(imMsgBizUser.getLastMsgTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
                conversation.setTimeStr(DateUtil.format(Date.from(imMsgBizUser.getLastMsgTime().atZone(ZoneId.systemDefault()).toInstant()), "yyyy/MM/dd"));
                conversation.setLatestMsgTime(Date.from(imMsgBizUser.getLastMsgTime().atZone(ZoneId.systemDefault()).toInstant()));
                conversation.setType(imMsgBizUser.getLastMsgType());
                conversation.setShopStatus(shopDetail.getShopStatus());
                conversation.setEmployeeId(imMsgBizUser.getEmployeeId());
                User user = userService.getUserByUserId(imMsgBizEmployeeRecent.getUserId());
                if (Objects.nonNull(user)) {
                    conversation.setPic(user.getPic());
                    conversation.setFriendName(user.getNickName());
                }
                conversations.add(conversation);
            }
        }
        Page<Conversation> conversationPage = new Page<>();
        BeanUtils.copyProperties(imMsgBizEmployeeRecentPage, conversationPage);
        conversationPage.setRecords(conversations);
        return ResponseEntity.ok(conversationPage);
    }

}
